package com.gm;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created by Administrator on 13-12-14.
 *
 * @author Gm
 * @version 0.1
 */
public class ImageViewFrame extends JFrame {
    public ImageViewFrame()
    {
        setTitle("Image Viewer");
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

        label=new JLabel();
        add(label);

        chooser=new JFileChooser(new File("."));
        FileNameExtensionFilter fileFilters=new FileNameExtensionFilter("Image Files","jpg","gif","png");
        chooser.setFileFilter(fileFilters);

        JMenuBar mainMenu=new JMenuBar();
        setJMenuBar(mainMenu);

        JMenu fileMenu=new JMenu("File");
        mainMenu.add(fileMenu);

        JMenuItem file_open=new JMenuItem("Open");
        file_open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result = chooser.showOpenDialog(null);

                if (result == JFileChooser.APPROVE_OPTION) {
                    String fileName=chooser.getSelectedFile().getPath();
                    label.setIcon(new ImageIcon(fileName));
                }

          }
        });
        fileMenu.add(file_open);

        JMenuItem file_exit=new JMenuItem("Exit");
        file_exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        fileMenu.add(file_exit);
        }

    private JLabel label;
    private JFileChooser chooser;
    private static  final int DEFAULT_WIDTH=400;
    private static final int DEFAULT_HEIGHT=300;
}
